<?php
require_once "../Common/database.php";
require_once "../Common/values.php";




$log=$_GET[log];


$assigned=$_POST['assigned'];
$query="SELECT * FROM tblJobs Where LogNumber='$_GET[log]'";
$row = mysql_fetch_array(mysql_query($query));

$query=mysql_query("Select * FROM tblstatus WHERE Name='$_POST[status]'");
$row = mysql_fetch_array($query);
$line=$row[Line];
if($_POST[solution] != "")
{
$query="INSERT INTO tblSolutions VALUES ('$_GET[log]','$date','$time','$user','$_POST[solution]','$_POST[times]','')";

mysql_query($query);
$query="INSERT INTO tblEvents VALUES ('$_GET[log]','$date','$time','$user','$_POST[solution]','$_POST[times]','')";

mysql_query($query);

$_POST[mail]='updated';
include "../Common/mail.php";

}
if($_POST['status']=='On Hold' AND $_POST[closed]=='checked')
{
 echo '<center><font color="red">Error:</font>Cannot close job when on hold <p> <a href="../index.php?p=hupdate&log='.$_GET[log].'">Back</a>';
exit;
}
if($_POST['status']=='1st Line - In Progress' AND $_POST[closed]=='checked')
{
$_POST[status]='1st Line';
}
if($_POST[closed]=='checked' && $_POST[solution]=='')
{
echo '<center><font color="red">Error:</font>Please enter a solution before closeing the job <p> <a href="../index.php?p=hupdate&log='.$_GET[log].'">Back</a>';
exit;
}

if($_POST[times]=='' && $_POST[solution]!='')
{
echo '<center><font color="red">Error:</font>Please enter Time spent before Posting a solution <p> <a href="../index.php?p=hupdate&log='.$_GET[log].'">Back</a>';
exit;
}


$query="SELECT * FROM tblJobs Where LogNumber='$_GET[log]'";
$row = mysql_fetch_array(mysql_query($query));

if($row[Status]!=$_POST[status])
{


$status='Status changed to '.$_POST[status];

$query="INSERT INTO tblSolutions VALUES ('$_GET[log]','$date','$time','$user','$status','0','')";
mysql_query($query);
}



if($row[Proirty]!=$_POST[proirty])
{

$status='Proirty changed to '.$_POST[proirty];
$query="INSERT INTO tblSolutions VALUES ('$_GET[log]','$date','$time','$user','$status','0','')";
mysql_query($query);
}

if($row[Assigned]!=$_POST[assigned])
{

$status='Assigned to '.$_POST[assigned];
$query="INSERT INTO tblSolutions VALUES ('$_GET[log]','$date','$time','$user','$status','0','')";
mysql_query($query);
$_POST[mail]='assigned';
require "../Common/mail.php";
}
if($row['Status']!=$_POST['status'] AND $_POST['status']=='1st Line - In Progress')
{

$status='Assigned to  '.$user;
$query="INSERT INTO tblSolutions VALUES ('$_GET[log]','$date','$time','$user','$status','0','')";
mysql_query($query);

$assigned=$user;

}
if($row[Closed]!=$_POST[closed])
{

if($_POST[closed]=='')
{
$status='Job Reopened';

$_POST[mail]='reopened';
require "../Common/mail.php";
}
if($_POST[closed]=='checked')
{

$status='Job Closed';

$query="INSERT INTO tblEvents VALUES ('$_GET[log]','$date','$time','$user','Closed Job','0','')";
mysql_query($query);

$_POST[mail]='closed';
require "../Common/mail.php";

}

$query="INSERT INTO tblSolutions VALUES ('$_GET[log]','$date','$time','$user','$status','0','')";
mysql_query($query);
}

if($row['Private']!=$_POST['private'])
{

if($_POST['private']=='')
{
$status='Job changed to Public';
}
if($_POST['private']=='checked')
{

$status='Job changed to Private';
}

$query="INSERT INTO tblSolutions VALUES ('$_GET[log]','$date','$time','$user','$status','0','')";
mysql_query($query);
}



$query="UPDATE tblJobs SET  Requestor='$_POST[requestor]',Phone='$_POST[phone]', VNC='$_POST[vnc]', Subject='$_POST[subject]',Proirty='$_POST[proirty]',Status='$_POST[status]',Problem='$_POST[problem]',Assigned='$assigned',Closed='$_POST[closed]',Line='$line', Private='$_POST[private]', Project='$_POST[project]', Due='$_POST[due]', Branch='$_POST[branch]', mail='$_POST[mailupdates]', Department='$_POST[department]' WHERE LogNumber = '$_GET[log]' ";

mysql_query($query);


if($_FILES["file"]["name"]!='')
{


if (file_exists("screenshots/" . $_FILES["file"]["name"]))
      {
      echo "<h2><center>".$_FILES["file"]["name"] . " File already exists. </h2>";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "screenshots/" . $_GET['log'].'.jpeg');
	$_POST[file]=$_FILES["file"]["name"];
      echo "<h2><center>File uploaded Stored in: " . "screenshots/".$_GET['log'].'.jpeg</h2>';

      $query="UPDATE tblJobs SET screenshot='".$_GET['log'].".jpeg' WHERE LogNumber='".$log."'";

mysql_query($query);
      }
}

if($_FILES["document"]["name"]!='')
{


if (file_exists("documents/" . $_FILES["document"]["name"]))
      {
      echo "<h2><center>".$_FILES["document"]["name"] . " File already exists. </h2>";
      }
    else
      {
      move_uploaded_file($_FILES["document"]["tmp_name"],
      "documents/" .$_FILES["document"]["name"]);
	$_POST[document]=$_FILES["document"]["name"];
      echo "<h2><center>File uploaded Stored in: " . "documents/".$_FILES["document"]["name"].'</h2>';

      $query="INSERT INTO tbldocuments Values ('$log','','$date','$time','$_POST[document]')";

mysql_query($query);
      }
}

if($_POST[status]=='Delete job')
    {
    $query="DELETE FROM tbljobs WHERE LogNumber='$_GET[log]'";
    mysql_query($query);
    $query="DELETE FROM tblsolutions WHERE LogNumber='$_GET[log]'";
    mysql_query($query);
    $query='OPTIMIZE TABLE `tbljobs` , `tblsolutions` ';
    mysql_query($query);
    echo '<center><h2>Job '.'<p>'.$_GET['log'].'<p> Deleted ';



echo '<p><a href="../index.php?p=hmy&f=helpdesk">Back to view jobs</a>';
exit;
    }



mysql_close();
echo '<center><h2>Job '.'<p>'.$_GET['log'].'<p> Updated ';


echo '<p><a href="../index.php?f=helpdesk&p=hupdate&log='.$_GET['log'].'">Back to Job</a>';
echo '<p><a href="../index.php?p=hmy&f=helpdesk">Back to view jobs</a>';


?>